﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BioBooking.Data.CtrLag
{
    public class MemberController
    {
        BioBookingContainer container;

        public MemberController()
        {
            container = new BioBookingContainer();
        }

        public void CreateMember(string fullName, string telePhone, string email)
        {
            Member member = new Member();
            member.FullName = fullName;
            member.Telephone = telePhone;
            member.Email = email;

            CreateMemberObject(member);
        }

        public void CreateMemberObject(Member member)
        {
            container.MemberSet.AddObject(member);
            container.SaveChanges();
        }

        public void UpdateMember(string fullName, string telePhone, string email)
        {
            Member member = new Member();
            member.FullName = fullName;
            member.Telephone = telePhone;
            member.Email = email;

            container.SaveChanges();
        }

        public void UpdateMemberObject(Member member)
        {
            container.SaveChanges();
        }

        public void DeleteMember(Member member)
        {
            container.MemberSet.DeleteObject(member);
            container.SaveChanges();
        }

        public Member SearchMember(string fullName)
        {
            return container.MemberSet.Where(p => p.FullName == fullName).SingleOrDefault();
        }

        public List<Member> listAllMembers()
        {
            return container.MemberSet.ToList<Member>();
        }

        public void SetDiscount(bool discount, int memberID)
        {
            container.MemberSet.FirstOrDefault(p => p.Id == memberID).Discount = discount;
        }

        public void SetCounter(Int32 counter, int memberID)
        {
            Member member = container.MemberSet.FirstOrDefault(p => p.Id == memberID);
            member.Counter++;
            
            if (member.Counter > 10)
            {
                member.Counter = 0;
                member.Discount = true;
            }
            container.SaveChanges();
        }
    }
}
